package com.zws.cucumber.snapshot;

import com.zws.cucumber.util.ZwsHttpUtil;
import com.zws.cucumber.util.colorfullog.ZwsColorfulLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ZwsMiscSnapshotManager implements ZwsSnapshotManager<Map<String, Object>> {

    private static final Logger logger = ZwsColorfulLogger.of(LoggerFactory.getLogger(ZwsMiscSnapshotManager.class));

    private ConcurrentMap<String, Map<String, Object>> snapshots = new ConcurrentHashMap<>();

    @Override
    public String name() {
        return "misc";
    }

    @Override
    public ConcurrentMap<String, Map<String, Object>> snapshots() {
        return snapshots;
    }

    @Override
    public void snapshot0(String snapshotName, Map<String, Object> args) {
        Map<String, Object> snapshot = new HashMap<>();
        snapshot.put("headers.customizedHeaders", ZwsHttpUtil.getCustomizedHeaders());
        snapshot.put("headers.removedHeaders", ZwsHttpUtil.getRemovedHeaders());

        snapshots.put(snapshotName, snapshot);
    }

    @Override
    public void restore0(String snapshotName, Map<String, Object> args) {
        Map<String, Object> snapshot = snapshots.get(snapshotName);

        ZwsHttpUtil.setCustomizedHeaders((Map<String, Set<String>>) snapshot.get("headers.customizedHeaders"));
        ZwsHttpUtil.setRemovedHeaders((Set<String>) snapshot.get("headers.removedHeaders"));

    }

    @Override
    public Logger logger() {
        return logger;
    }
}
